#include "lcd.h"

/* MOD */
/* 设定按键引脚 */
#define Pin_Key P17

/************************************************
 *                                              *
 *              Place Lcd Code                  *
 *                                              *
 ************************************************/
/* MOD */
/* 在这里个区域放原来的点屏程序 */
unsigned char code iconfull[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
				 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

unsigned char code iconClearArr[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				     0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

unsigned char code iconodd[] = {0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
				0x01, 0x00, 0x01, 0x00, 0x01, 0x00};

unsigned char code iconevn[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
				0x00, 0x01, 0x00, 0x01, 0x00, 0x01};
unsigned char contrast = 49;

#define cs1 P10
#define rst P11
#define rs P12
#define sck P13
#define sda P14

// serial 4-SPI
void write_com(unsigned char byte)
{
	unsigned char i;

	rs = 0;
	for (i = 0; i < 8; i++) {
		sck = 0;
		_nop_();
		_nop_();
		sda = (bit)(byte & 0x80);
		byte = byte << 1;
		sck = 1;
		_nop_();
		_nop_();
	}
}

// serial 4-SPI
void write_dat(unsigned char byte)
{
	unsigned char i;
	rs = 1;
	for (i = 0; i < 8; i++) {
		sck = 0;
		_nop_();
		_nop_();
		sda = (bit)(byte & 0x80);
		sck = 1;
		byte = byte << 1;
		_nop_();
		_nop_();
	}
}

void ST7571_YMCO0921BAAFUGN_reset_lcm(void)
{
	cs1 = 0;
	rst = 0; //模块复位
	delay_ms(100);
	rst = 1;
}

void ST7571_YMCO0921BAAFUGN_init_lcm(void)
{
	ST7571_YMCO0921BAAFUGN_reset_lcm();

	write_com(0xae);
	write_com(0x38); // FR FREQUENCY
	write_com(0x7C); // 70 HZ;4 Booster

	write_com(0x48); // display duty select:
	write_com(110);	 // duty=111

	write_com(0xa1); // Set SEG Scan Direction: seg127-seg0
	write_com(0xc8); // Set COM Scan Direction: com96-com0
	write_com(0x40); // initial display line
	write_com(0x00);
	write_com(0x44); // com0 register select:
	write_com(0x00); // com0=0 line

	write_com(0xab); // oscillator on start
	write_com(0x67);
	write_com(0x27);     // regulator resistor select: 1+Rb/Ra=26H
	write_com(0x81);     // electronic volume register select:
	write_com(contrast); //实测14.01-14.11V 理论值14.04V
	delay_ms(50);

	write_com(0x56); // bias select(0x54:1/9;   0x55:1/10;   0x56:1/11)
	write_com(0xf3);
	write_com(0x04);
	write_com(0x93);
	write_com(0x2C); // Power control
	delay_ms(50);
	write_com(0x2e);
	delay_ms(50);
	write_com(0x2f);
	delay_ms(50);

	write_com(0xaf); // display on

	write_com(0xa5); // display full
	write_com(0xa4); // Entire Display OFF
	delay_ms(50);
}

//第一页图素写入
void ST7571_YMCO0921BAAFUGN_clearPage0(unsigned char da)
{
	unsigned char j;

	write_com(0xb0);
	write_com(0x10);
	write_com(0x00);

	for (j = 0; j < 128; j++) // 128列为例
	{
		write_dat(da);
		write_dat(da);
	}
}

// page 0-14 clear
void ST7571_YMCO0921BAAFUGN_clearPage_0_14(unsigned char da)
{
	unsigned char i, j;

	for (i = 0xb0; i < 0xbe; i++) // 96行
	{
		write_com(i);
		write_com(0x10);
		write_com(0x00);

		for (j = 0; j < 128; j++) // 128列为例
		{
			write_dat(da);
			write_dat(da);
		}
	}
}

void ST7571_YMCO0921BAAFUGN_clearPageOddEvn(unsigned char dodd,
					    unsigned char devn)
{
	unsigned char i, j;

	for (i = 0xb0; i <= 0xbf; i++) // 96行
	{
		write_com(i);
		write_com(0x10);
		write_com(0x00);

		for (j = 0; j < 128 / 2; j++) // 128列为例
		{
			write_dat(dodd);
			write_dat(dodd);
			write_dat(devn);
			write_dat(devn);
		}
	}
}

void ST7571_YMCO0921BAAFUGN_clearPage1(
    unsigned char da) //第二页图素写入，折这页是中间的那页
{
	unsigned char j;

	write_com(0xb1);
	write_com(0x10);
	write_com(0x00);

	for (j = 0; j < 128; j++) // 128列为例
	{
		write_dat(da);
		write_dat(da);
	}
}

//分揽开----------------------------------------------------------------------------------
void ST7571_YMCO0921BAAFUGN_clearPage_2_14(unsigned char da)
{
	unsigned char i, j;

	for (i = 0xb2; i < 0xbe; i++) // 96行
	{
		write_com(i);
		write_com(0x10);
		write_com(0x00);

		for (j = 0; j < 128; j++) // 128列为例
		{
			write_dat(da);
			write_dat(da);
		}
	}
}

void ST7571_YMCO0921BAAFUGN_clearPage_1(unsigned char da, unsigned char db,
					unsigned char dc,
					unsigned char dd) //针对中间页
{
	unsigned char i;

	write_com(0xb1);
	write_com(0x10);
	write_com(0x00);
	for (i = 0; i < 32; i++) // 128列为例，128/4=32
	{
		write_dat(da);
		write_dat(da);
		write_dat(db);
		write_dat(db);
		write_dat(dc);
		write_dat(dc);
		write_dat(dd);
		write_dat(dd);
	}
}

//
void ST7571_YMCO0921BAAFUGN_clearPage_2_15(unsigned char da, unsigned char db,
					   unsigned char dc, unsigned char dd)
{
	unsigned char i, j;
	for (j = 2; j < 15; j++) {
		write_com(0xb0 + j); // 64行为例
		write_com(0x10);
		write_com(0x00);
		for (i = 0; i < 32; i++) // 128列为例，128/4=32
		{
			write_dat(da);
			write_dat(da);
			write_dat(db);
			write_dat(db);
			write_dat(dc);
			write_dat(dc);
			write_dat(dd);
			write_dat(dd);
		}
	}
}

//图形显示
void ST7571_YMCO0921BAAFUGN_lcd_image(unsigned char code *p)
{
	unsigned char i, j;
	for (i = 0xb0; i < 0xbE; i++) {
		write_com(i);	 // RAM page address
		write_com(0x10); // RAM column address most address
		write_com(0x00); // RAM column address least address
		for (j = 0; j < 128; j++) {
			write_dat(*p);
			write_dat(*p++);
		}
	}
}

void ST7571_YMCO0921BAAFUGN_lcd_icon(unsigned char code *p)
{
	unsigned char j;
	write_com(0xa3); // RAM page address
	write_com(0x10); // RAM column address most address
	write_com(0x00); // RAM column address least address
	for (j = 0; j < 12; j++) {
		write_dat(*p);
		write_dat(*p++);
	}
}

void ST7571_YMCO0921BAAFUGN_iconClear(unsigned char odd_d0, unsigned evn_d0)
{
	unsigned char j;
	write_com(0xa3);	// RAM page address
	write_com(0x10);	// RAM column address most address
	write_com(0x00);	// RAM column address least address
	for (j = 0; j < 9; j++) // 17/2+1
	{
		write_dat(odd_d0);
		write_dat(evn_d0);
	}
}

void ST7571_YMCO0921BAAFUGN_drawByte(unsigned char clm, unsigned char page,
				     unsigned char data1, unsigned char data2)
{

	write_com(page);		     // RAM page address
	write_com(0x10 | (clm & 0x0f));	     // RAM column address most address
	write_com(0x00 | (clm >> 4) & 0x0f); // RAM column address least address

	write_dat(data1);
	write_dat(data2);
}

void ST7571_YMCO0921BAAFUGN_Test(void)
{
	delay_us(0);
	delay_ms(0);
	delay_ss(0);
	// isp_init();

	P4 = 0x00;

	ST7571_YMCO0921BAAFUGN_reset_lcm();
	cs1 = 0;
	ST7571_YMCO0921BAAFUGN_init_lcm();
	// while (1) {
	// drawByte(0, 0, 0, 0);

	switch (2) {
	case 0:
		/*lcd_icon(iconfull);
		clearPage_0_14(0xff); //全显
		delay_ms(250);

		// vertical
		lcd_icon(
		    iconClear); //清ICON，不然上面写的全显下面还显示
		clearPage0(0x00);
		clearPage_1(0xe0, 0x00, 0x00, 0x00);
		clearPage_2_15(0xff, 0x00, 0x00, 0x00);
		delay_ms(200);
		clearPage_1(0x00, 0xe0, 0x00, 0x00);
		clearPage_2_15(0x00, 0xff, 0x00, 0x00);
		delay_ms(200);
		clearPage_1(0x00, 0x00, 0xe0, 0x00);
		clearPage_2_15(0x00, 0x00, 0xff, 0x00);
		delay_ms(200);
		clearPage_1(0x00, 0x00, 0x00, 0xe0);
		clearPage_2_15(0x00, 0x00, 0x00, 0xff);
		delay_ms(200);

		lcd_icon(
		    iconClear); //清ICON，不然上面写的全显下面还显示
		clearPage0(0x00);
		clearPage_1(0xe0, 0x00, 0xff, 0x00);
		clearPage_2_15(0xff, 0x00, 0xff, 0x00);
		delay_ms(200);
		clearPage_1(0x00, 0xe0, 0x00, 0xff);
		clearPage_2_15(0x00, 0xff, 0x00, 0xff);
		delay_ms(200);

		// horizontal
		clearPage0(0x00);
		clearPage1(0x40);
		clearPage_2_14(0x44);
		delay_ms(200);
		clearPage1(0x80);
		clearPage_2_14(0x88);
		delay_ms(200);
		clearPage1(0x00);
		clearPage_2_14(0x11);
		delay_ms(200);
		clearPage1(0x00);
		clearPage_2_14(0x22);
		delay_ms(200);

		lcd_image(image_yes); //图形
		//		lcd_icon(icon_clc);
		delay_ms(250);

		//		 lcd_icon(icon1);
		//		lcd_image(image_yes1);//图形
		//    //lcd_icon(icon1);
		//		delay_ms(250);

		//			lcd_icon(icon2);
		//		lcd_image(image_yes2);//图形
		//		//lcd_icon(icon2);
		//		delay_ms(250);
		//
		//		 lcd_icon(icon3);
		//		lcd_image(image_yes3);//图形
		//    //lcd_icon(icon3);
		//		delay_ms(250);

		lcd_icon(icon4);
		lcd_image(image_yes4); //图形
		delay_ms(250);

		lcd_icon(icon3);
		lcd_image(image_yes3); //图形
		delay_ms(250);

		lcd_icon(icon2);
		lcd_image(image_yes2); //图形
		delay_ms(250);

		lcd_icon(icon1);
		lcd_image(image_yes1); //图形
		delay_ms(250);

		/*cs1=1;
		lcd_image(image_cs);delay_ms(250);
		cs1=0;

		write_com(0xa9);*/

		//		cs1=1;
		//		lcd_image(image_cs);delay_ms(250);
		//		cs1=0;*/
		break;

	case 1:
		// lcd_icon(iconful);//清ICON，不然上面写的全显下面还显示
		// clearPage0(0x00);

		ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xff, 0x00);
		delay_ms(200);
		ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x00, 0xff);
		delay_ms(200);
		ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x55, 0x55);
		delay_ms(200);
		ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xaa, 0xaa);
		delay_ms(200);
		ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x33, 0x33);
		delay_ms(200);
		ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xcc, 0xcc);
		delay_ms(200);
		break;

	case 2:
		ST7571_YMCO0921BAAFUGN_clearPage_0_14(0x00);

		ST7571_YMCO0921BAAFUGN_lcd_icon(iconfull);
		delay_ms(800);

		ST7571_YMCO0921BAAFUGN_lcd_icon(iconClearArr);
		delay_ms(800);

		// iconClear(0x01, 0x00);
		ST7571_YMCO0921BAAFUGN_lcd_icon(iconodd);
		delay_ms(800);

		// iconClear(0x00, 0x01);
		ST7571_YMCO0921BAAFUGN_lcd_icon(iconevn);
		delay_ms(800);

		break;

	default:
		break;
	}
	//}
}

/************************************************
 *                                              *
 *              Invoke Lcd Code                 *
 *                                              *
 ************************************************/
/* MOD */
/* 在这里个区域调用原来的各个显示画面 */

// get key io value
unsigned char aoiGetKey(void)
{
	unsigned char keyVal = Pin_Key;
	return keyVal;
}

void aoiSetLcdInterface(void)
{
	// TODO
}

// 调用模块初始化函数, 初始化函数要包含复位过程
void aoiLcdInitial(void) { ST7571_YMCO0921BAAFUGN_init_lcm(); }

// 把原来的main()函数改一个名字, 放在前面, 在这里调用那个原来的main函数
void aoiLcdTest(void) { ST7571_YMCO0921BAAFUGN_Test(); }
/* 以下的显示画面函数都不要带延迟 */

// 调用测试画面
// 全显
void aoiLcdImageF(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xff, 0xff); }

// 调用测试画面
// 清屏
void aoiLcdImage0(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x00, 0x00); }

// 调用测试画面
void aoiLcdImage1(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xff, 0x00); }

// 调用测试画面
void aoiLcdImage2(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x00, 0xff); }

// 调用测试画面
void aoiLcdImage3(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x55, 0x55); }

// 调用测试画面
void aoiLcdImage4(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xaa, 0xaa); }

// 调用测试画面
void aoiLcdImage5(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0x33, 0x33); }

// 调用测试画面
void aoiLcdImage6(void) { ST7571_YMCO0921BAAFUGN_clearPageOddEvn(0xcc, 0xcc); }

// 调用测试画面
void aoiLcdImage7(void)
{
	ST7571_YMCO0921BAAFUGN_clearPage_0_14(0x00);
	ST7571_YMCO0921BAAFUGN_lcd_icon(iconfull);
}

// 调用测试画面
void aoiLcdImage8(void)
{
	ST7571_YMCO0921BAAFUGN_clearPage_0_14(0x00);
	ST7571_YMCO0921BAAFUGN_lcd_icon(iconodd);
}

void aoiLcdImage9(void) { ST7571_YMCO0921BAAFUGN_lcd_icon(iconevn); }

void aoiLcdImageA(void) {}

void aoiLcdImageB(void) {}

void aoiLcdImageC(void) {}

void aoiLcdImageD(void) { ST7571_YMCO0921BAAFUGN_Test(); }
